package com.pig4cloud.pig.module.controller.gate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.gate.BarrierGate;
import com.pig4cloud.pig.module.api.entity.gate.BarrierGateRecord;
import com.pig4cloud.pig.module.api.entity.platform.PlatformPay;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.gate.IBarrierGateRecordService;
import com.pig4cloud.pig.module.service.gate.IBarrierGateService;
import com.pig4cloud.pig.module.service.platform.IPlatformPayService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 道闸的进出记录Controller
 *
 * @author ruoyi
 * @date 2023-06-12
 */
@Validated
@Api(value = "道闸的进出记录控制器", tags = {"道闸的进出记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/barrier/gate/record")
public class BarrierGateRecordController {

    private final IBarrierGateRecordService barrierGateRecordService;
    private final IBarrierGateService barrierGateService;
    private final IWxUserService wxUserService;
    private final IPlatformPayService platformPayService;

    /**
     * 查询道闸的进出记录列表
     */
    @ApiOperation("查询道闸的进出记录列表")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<BarrierGateRecord> list(@Validated BarrierGateRecord barrierGateRecord) {
        QueryWrapper<BarrierGateRecord> qw = new QueryWrapper<>();
        if (barrierGateRecord.getGateId()!=null){
            qw.eq("gate_id",barrierGateRecord.getGateId());
        }
        TableDataInfo<BarrierGateRecord> td = PageUtils.buildDataInfo(barrierGateRecordService.page(PageUtils.buildPage(),qw));
        List<BarrierGateRecord> list = td.getData();
        for (BarrierGateRecord list1:list){
            if (list1.getGateId()!=null){
                BarrierGate barrierGate = barrierGateService.getById(list1.getGateId());
                if (barrierGate!=null){
                    list1.setGateName(barrierGate.getGateName());
                }
            }
            if (list1.getWxUserId()!=null){
                WxUser wxUser = wxUserService.getById(list1.getWxUserId());
                if (wxUser!=null){
                    list1.setFaceUrl(wxUser.getFaceUrl());
                    list1.setNickName(wxUser.getNickName());
                }
            }
            if (list1.getPayPlatformId()!=null){
                PlatformPay platformPay = platformPayService.getById(list1.getPayPlatformId());
                if (platformPay!=null){
                    list1.setPayPlatformName(platformPay.getPlatformName());
                }
            }
        }
        return td;
    }

    /**
     * 导出道闸的进出记录列表
     */
    @ApiOperation("导出道闸的进出记录列表")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:export')")
    @SysLog("道闸的进出记录")
    @GetMapping("/export")
    public List export(@Validated BarrierGateRecord barrierGateRecord) {
        List<BarrierGateRecord> list = barrierGateRecordService.list();
        for (BarrierGateRecord list1:list){
            if (list1.getGateId()!=null){
                BarrierGate barrierGate = barrierGateService.getById(list1.getGateId());
                if (barrierGate!=null){
                    list1.setGateName(barrierGate.getGateName());
                }
            }
            if (list1.getWxUserId()!=null){
                WxUser wxUser = wxUserService.getById(list1.getWxUserId());
                if (wxUser!=null){
                    list1.setNickName(wxUser.getNickName());
                }
            }
            if (list1.getPayPlatformId()!=null){
                PlatformPay platformPay = platformPayService.getById(list1.getPayPlatformId());
                if (platformPay!=null){
                    list1.setPayPlatformName(platformPay.getPlatformName());
                }
            }
            if (list1.getCarNumType()!=null){
                if (list1.getCarNumType()==0){
                    list1.setCarNumTypes("未知车牌");
                }
                if (list1.getCarNumType()==1){
                    list1.setCarNumTypes("蓝牌小汽车");
                }
                if (list1.getCarNumType()==2){
                    list1.setCarNumTypes("黑牌小汽车");
                }
                if (list1.getCarNumType()==3){
                    list1.setCarNumTypes("单排黄牌");
                }
                if (list1.getCarNumType()==4){
                    list1.setCarNumTypes("双排黄牌");
                }
                if (list1.getCarNumType()==5){
                    list1.setCarNumTypes("警车车牌");
                }
                if (list1.getCarNumType()==6){
                    list1.setCarNumTypes("武警车牌");
                }
                if (list1.getCarNumType()==7){
                    list1.setCarNumTypes("个性化车牌");
                }
                if (list1.getCarNumType()==8){
                    list1.setCarNumTypes("单排军车牌");
                }
                if (list1.getCarNumType()==9){
                    list1.setCarNumTypes("双排军车牌");
                }
                if (list1.getCarNumType()==10){
                    list1.setCarNumTypes("使馆车牌");
                }
                if (list1.getCarNumType()==11){
                    list1.setCarNumTypes("香港进出中国大陆车牌");
                }
                if (list1.getCarNumType()==12){
                    list1.setCarNumTypes("农用车牌");
                }
                if (list1.getCarNumType()==13){
                    list1.setCarNumTypes("教练车牌");
                }
                if (list1.getCarNumType()==14){
                    list1.setCarNumTypes("澳门进出中国大陆车牌");
                }
                if (list1.getCarNumType()==15){
                    list1.setCarNumTypes("双层武警车牌");
                }
                if (list1.getCarNumType()==16){
                    list1.setCarNumTypes("武警总队车牌");
                }
                if (list1.getCarNumType()==17){
                    list1.setCarNumTypes("双层武警总队车牌");
                }
                if (list1.getCarNumType()==18){
                    list1.setCarNumTypes("民航车牌");
                }
                if (list1.getCarNumType()==19){
                    list1.setCarNumTypes("新能源车牌");
                }
            }
            if (list1.getPayState()!=null){
                if (list1.getPayState()==0){
                    list1.setPayStates("未支付");
                }
                if (list1.getPayState()==1){
                    list1.setPayStates("待支付");
                }
                if (list1.getPayState()==2){
                    list1.setPayStates("已支付");
                }
            }
            if (list1.getInTime()!=null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                list1.setInTimes(sdf.format(list1.getInTime()));
            }
            if (list1.getOutTime()!=null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                list1.setOutTimes(sdf.format(list1.getOutTime()));
            }
        }
        // ExcelUtil<BarrierGateRecord> util = new // ExcelUtil<BarrierGateRecord>(BarrierGateRecord.class);
        return  list;// util.exportExcel(list, "道闸的进出记录");
    }

    /**
     * 获取道闸的进出记录详细信息
     */
    @ApiOperation("获取道闸的进出记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:query')")
    @GetMapping("/{id}")
    public R<BarrierGateRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        BarrierGateRecord barrierGateRecord = barrierGateRecordService.getById(id);
        return R.ok(barrierGateRecord);
    }

    /**
     * 新增道闸的进出记录
     */
    @ApiOperation("新增道闸的进出记录")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:add')")
    @SysLog("道闸的进出记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody BarrierGateRecord barrierGateRecord) {
        return R.okByRow(barrierGateRecordService.save(barrierGateRecord) ? 1 : 0);
    }

    /**
     * 修改道闸的进出记录
     */
    @ApiOperation("修改道闸的进出记录")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:edit')")
    @SysLog("道闸的进出记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody BarrierGateRecord barrierGateRecord) {
        return R.okByRow(barrierGateRecordService.updateById(barrierGateRecord) ? 1 : 0);
    }

    /**
     * 删除道闸的进出记录
     */
    @ApiOperation("删除道闸的进出记录")
    @PreAuthorize("@pms.hasPermission('module:barrierGateRecord:remove')")
    @SysLog("道闸的进出记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(barrierGateRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
